<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <button type="button" class="btn btn-info" onclick="i_w_importword()">导入词汇</button>
    <br/>
    <div id="xiazaipaths"></div>


    <script>
        var upload_file = webrootpath+"file/upload_file"; //上传文件
        var dispose_and_xiazailink = webrootpath+"test/dispose_and_xiazailink";
        var randomstr = Math.ceil(Math.random()*9999999); //随机种子
        var get_initstatuslink = webrootpath+"test/get_initstatuslink";


        $(function () {

        });
        /***
         * 处理并生成下载文件
         * */
        function dispose_and_xiazai() {
            $("#init_loading").modal("show");

            var param_1 = $.trim($("#param_1").val());
            var param_2 = $.trim($("#param_2").val());
            var att_name_list = "";
            $("input[name='att_name']").each(function (_index,_this) {
                att_name_list+=","+_this.value;
            });


            if(att_name_list==""){
                return false;
            }
            var isjishi = setInterval(function () {
                get_initstatus(randomstr);
            },3000);



            $.ajax({
                type:"post",
                url:dispose_and_xiazailink,
                data:{
                    param_1:param_1,
                    param_2:param_2,
                    att_name_list:att_name_list,
                    randomstr:randomstr
                },
                async:true,
                timeout:9999999,
                success:function (_info) {
                    $("#i_w_importword_dialog").modal("hide");
                    if (_info.code=="1"){
                        $("#xiazaipaths").html(_info.data);
                        $("#init_loading").modal("hide");
                        window.clearInterval(isjishi);
                    }
                },
                error:function (_msg) {
                    console.log(_msg);
                }

            });
        }

        function get_initstatus(_random) {
            $.get(get_initstatuslink,{
                random:_random
            },function (_info) {
                if(_info.code=="1"){
                    $("#init_loading_zhixingzhuangtai").html(_info.msg);
                }
            });
        }


        
        
        /***
         * 批量导入词表_弹出框
         */
        function i_w_importword() {
            $("#i_w_importword_dialog").modal("show");
            setTimeout(function () {
                $("#i_w_importword_picker").html("选择附件");
                loadworduploader();
            },500);
        }

        /***
         * 加载上传组件
         */
        function loadworduploader() {
            $("#i_w_importword_uploadlist").html("");
            $("#i_w_importword_list").html("");

            var uploader = WebUploader.create({
                swf: '../webuploader-0.1.5/Uploader.swf',
                auto:true,
                server: upload_file,
                pick: '#i_w_importword_picker',
                resize: false,
                fileNumLimit:1,
                formData:{
                    filetype:10
                },
                accept:{
                    title: '文本内容',
                    extensions: 'xlsx',
                    mimeTypes: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
                },
                fileSizeLimit:10485760000
            });
            uploader.on( 'fileQueued', function( file ) {

                $("#i_w_importword_uploadlist").html( '<div id="' + file.id + '" class="item">' +
                    '<h4 class="info">' + file.name + '</h4>' +
                    '<p class="state">等待上传...</p>' +
                    '</div>' );
            });
            uploader.on( 'beforeFileQueued', function( file ) {
                uploader.reset();
            });

            uploader.on( 'uploadProgress', function( file, percentage ) {
                var $li = $( '#'+file.id ),
                    $percent = $li.find('.progress .progress-bar');

                if ( !$percent.length ) {
                    $percent = $('<div class="progress progress-striped active">' +
                        '<div class="progress-bar" role="progressbar" style="width: 0%">' +
                        '</div>' +
                        '</div>').appendTo( $li ).find('.progress-bar');
                }

                $li.find('p.state').text('上传中并正在处理，请稍等！！！');
                $percent.css( 'width', percentage * 100 + '%' );
            });
            //上传成功
            uploader.on( 'uploadSuccess', function( file,response ) {
                $( '#'+file.id ).find('p.state').text('上传完毕');

                setTimeout(function(){
                    if(response.code=='1'){
                        var htmlstr = "<tr id=\"att"+response.data.attachedid+"\" >"+
                            "<td>"+response.data.truename+"<input name='att_name' value='"+response.data.attachedid+"' style='display: none;' /></td>"+
                            "<td>"+response.data.size+"</td>"+
                            "<td><span class=\"label label-danger\" style='cursor: pointer;'  onclick=\"delAttached('"+response.data.attachedid+"')\">删除</span></td>"+
                            "</tr>";

                        $("#i_w_importword_list").append(htmlstr);
                    }
                    $("#i_w_importword_uploadlist").html("");
                    uploader.reset();
                },1000);

            });
            uploader.on( 'uploadError', function( file ) {
                $( '#'+file.id ).find('p.state').text('上传出错');
                uploader.reset();
            });
            uploader.on( 'uploadComplete', function( file ) {
                $( '#'+file.id ).find('.progress').fadeOut();
            });
        }

    </script>

</body>
</html>